package com.example.springboot_demo.service;

import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.Set;

/**
 * 判断对象是否为空
 * @ClassName EmptyUtils 
 * @author xiaojy
 * @version V1.0  
 * @since V1.0 
 * @date 2017年10月26日 上午11:17:08 
 *
 */
@SuppressWarnings("rawtypes")
public class EmptyUtils {

	/**
	 * 判断对象不为空
	 * @param o 目前支持 Object,Collection,Map,String,数组,set
	 * @return 不为空返回true 为空返回false
	 */
	public static boolean isNotEmpty(Object o) {
		return !isEmpty(o);
	}
	
	/**
	 * 判断对象为空
	 * @param o 目前支持 Object,Collection,Map,String,数组,set
	 * @return 空返回true 不为空返回false
	 */
	public static boolean isEmpty(Object o) {
		if (o == null){
			return true;
		} else if (o instanceof Collection) {
			return ((Collection) o).isEmpty();
		} else if (o instanceof Map) {
			return ((Map) o).isEmpty();
		} else if (o instanceof String) {
			return ((String) o).trim().length() <= 0;
		} else if (o.getClass().isArray()){
			return Array.getLength(o) <= 0;
		} else if (o instanceof Set) {
            return ((Set) o).isEmpty();
        }
		return false;
	}

	/**
	 * @description 全部为空
	 * @param obj
	 * @return boolean
	 */
	public static boolean allEmpty(Object... obj) {
		for (Object o : obj) {
			if (isNotEmpty(o)) {
				return false;
			}
		}
		return true;
	}

	/**
	 * @description 全部不为空
	 * @param obj
	 * @return boolean
	 */
	public static boolean allNotEmpty(Object... obj) {
		for (Object o : obj) {
			if (isEmpty(o)) {
				return false;
			}
		}
		return true;
	}

	public static boolean isNotEmptyObject(Object o) {
	    return !isEmptyObject(o);
    }

    /**
     * 通过对象的属性判断对象是否为空，所有属性都为空时返回true
     * @param o
     */
	public static boolean isEmptyObject(Object o) {
	    if (null == o)  {
	        return true;
        }

        try {
            for (Field f : o.getClass().getDeclaredFields()) {
                f.setAccessible(true);
                if (f.get(o) != null && StringUtils.isNotBlank(f.get(o).toString())) {
                    return false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return true;
    }
	/**
	 * 通过对象的属性判断对象是否为空，所有属性都为空时返回true，可以指定排除字段
	 * @param o
	 * @param exclude 排除的字段
	 */
	public static boolean isEmptyObjectExclude(Object o, String... exclude) {
		if (null == o)  {
			return true;
		}

		try {
			for (Field f : o.getClass().getDeclaredFields()) {
				f.setAccessible(true);
				Object oVal = f.get(o);
				String filedName = f.getName();
				if (isNotEmpty(exclude)) {
					if(Arrays.asList(exclude).contains(filedName)) {
						continue;
					}
				}
				if (oVal != null && StringUtils.isNotBlank(oVal.toString())) {
					return false;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return true;
	}

	/**
	 * 通过对象的属性判断对象是否为空，所有属性都为空时返回true，只判断指定字段
	 * @param o
	 * @param include 排除的字段
	 */
	public static boolean isEmptyObjectInclude(Object o, String... include) {
		if (null == o)  {
			return true;
		}

		try {
			for (Field f : o.getClass().getDeclaredFields()) {
				f.setAccessible(true);
				Object oVal = f.get(o);
				String filedName = f.getName();
				if (isNotEmpty(include)) {
					if(Arrays.asList(include).contains(filedName)){
						if (oVal != null && StringUtils.isNotBlank(oVal.toString())) {
							return false;
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return true;
	}

	public static boolean isNotEmptyObjectExclude(Object o, String... exclude) {
		return !isEmptyObjectExclude(o, exclude);
	}
	public static boolean isNotEmptyObjectInclude(Object o, String... include) {
		return !isEmptyObjectInclude(o,include);
	}

	public static boolean isNull(Object o){
		if (o == null){
			return true;
		} else if (o instanceof Collection) {
			return ((Collection) o).isEmpty();
		} else if (o instanceof Map) {
			return ((Map) o).isEmpty();
		} else if (o.getClass().isArray()){
			return Array.getLength(o) <= 0;
		} else if (o instanceof Set) {
            return ((Set) o).isEmpty();
        }
		return false;
	}

	public static boolean isNotNull(Object o){
		return !isNull(o);
	}
	/*
	public static void main(String[] args) {
		String str = "        ";
		List list = new ArrayList();
		//list.add("123");
		Map map = new HashMap();
		//map.put("22", "123");
		Object o = null;
		String[] s = {};
		double[] d = {};
		int[] i = {};
		Set set = new HashSet();

		System.out.println(isEmpty(str));
		System.out.println(isEmpty(list));
		System.out.println(isEmpty(map));
		System.out.println(isEmpty(o));
		System.out.println(isEmpty(s));
		System.out.println(isEmpty(d));
		System.out.println(isEmpty(i));
		System.out.println(isEmpty(set));
	}
	*/
}
